﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Collections.TreeCollections
{
    public class TreeNodeIdentify : ICloneable
    {
        #region Fields
        private static Random _random = new Random();
        private static readonly int _RANDOM_MAX = 99999999;
        private static readonly int _RANDOM_MIN = 0;
        private string _idString;
        private readonly char _SPLIT_SYMBOL = '_';
        #endregion

        #region Constructor
        /// <summary>
        /// 
        /// </summary>
        /// <param name="idString"></param>
        public TreeNodeIdentify()
        {
            StringBuilder strBuild = new StringBuilder();
            int[] num = new int[4];
            for (int i = 0; i != 4; ++i)
            {
                num[i] = _random.Next(_RANDOM_MIN, _RANDOM_MAX);
            }
            foreach (int n in num)
            {
                strBuild.AppendFormat("{0}{1}"
                    , turnNumber2Str((uint)n)
                    , _SPLIT_SYMBOL);
            }
            _idString = string.Format("Id{0}", strBuild.ToString().TrimEnd(_SPLIT_SYMBOL));
        }
        /// <summary>
        /// 
        /// </summary>
        public TreeNodeIdentify(string idStr)
        {
            this._idString = idStr;
        }
        #endregion

        /// <summary>
        /// To String
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return _idString;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private string turnNumber2Str(uint number)
        {
            StringBuilder strBuilder = new StringBuilder();
            if (number >= _RANDOM_MAX)
            {
                throw new Exception("Number over range!");
            }
            string numStr = number.ToString();
            for (int i = 0; i != 8 - numStr.Length; ++i)
            {
                strBuilder.Append('0');
            }
            for (int i = 0; i != numStr.Length; ++i)
            {
                strBuilder.Append(numStr[i]);
            }
            return strBuilder.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            return new TreeNodeIdentify(this._idString);
        }

        #region Equals
        /// <summary>
        /// Determines whether the Sepecified FeatureModel.Collections.TreeCollections.TreeNodeIdentify
        /// equals to current FeatureModel.Collections.TreeCollections.TreeNodeIdentify
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            TreeNodeIdentify id = obj as TreeNodeIdentify;
            if (id == null)
            {
                return false;
            }
            return this._idString == id._idString;
        }
        /// <summary>
        /// Return base.GetHashCode()
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion
    }
}
